<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms as Smslib;
use think\Db;
use think\Hook;

/**
 * 主播机器人接口
 */
class LivestreamerRobot extends Api
{
    protected $noNeedLogin = ['user_logn','vipuser_add','user_register','bind_tiktok_user','task_box_add'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('app\api\model\LivestreamerList');
    }

    //1登录=========================
    public function user_logn(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "mobile",
            "tiktokusername",
            "pwdMd5",
            "IP",
            "followNum",
            "fansNum",
            "likesNum",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userGrade",
            "zone",
            "res",
        ];
        //存储过程需要密码md5加密
        $params['pwdMd5'] = md5($params['pwdMd5']);
        //查询
        $procedure_name = 'user_login';
        $res = $this->model->procedure_data_select($params,$must_param,$procedure_name,$out_column);

        if($res[0]['res']){
            $account = $this->request->post('mobile');
            $password = $this->request->post('pwdMd5');
            if (!$account || !$password) {
                $this->error(__('Invalid parameters'),[],1);
            }
            $ret = $this->auth->login2($account, $password);
            if ($ret) {
                $userinfo = $this->auth->getUserinfo();

                $userinfo['userGrade'] = $res[0]['userGrade'];
                $userinfo['zone'] = $res[0]['zone'];
                $userinfo['res'] = $res[0]['res'];
                $data = ['userinfo' => $userinfo];

                $this->success(__('Logged in successful'), $data);
            } else {
                $this->error($this->auth->getError(),[],1);
            }
        }else{
            $this->error(__('登录失败'),[],1);
        }

        $this->success('返回成功', $res);
    }

    //1.2-注册
    public function user_register(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "mobile",
            "pwdMd5",
            "language",
            "IP",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        //存储过程需要密码md5加密
        $params['pwdMd5'] = md5($params['pwdMd5']);

        $procedure_name = 'user_add';
        $rs = $this->model->procedure_data_add($params,$must_param,$procedure_name);
        my_debug(1,'rs--94');
        my_debug(1,$rs);
        if($rs['res']){
            $this->success('注册成功', []);
        }else{
            $this->error(__('用户已存在'),[],1);
        }
    }




    //2-绑定tiktok账户
    public function bind_tiktok_user(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "mobile",
            "pwdMd5",
            "tiktokusername",
            "tiktoknickname",
            "zone",
            "photoURL",
            "followNum",
            "fansNum",
            "likesNum",
            "signature",
            "twitter_id",
            "twitter_name",
            "youtube_channel_id",
            "youtube_channel_title",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $params['pwdMd5'] = md5($params['pwdMd5']);
        $procedure_name = 'user_bind_tiktok';
        $rs = $this->model->procedure_data_add($params,$must_param,$procedure_name);
        if($rs['res']){
            $this->success('绑定成功', []);
        }else{
            $this->error(__('绑定失败'),[],1);
        }
    }

    //3-登记主播直播环境
    public function user_login_env(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "IP",
            "language",
            "timezone",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('设置成功', []);
        }else{
            $this->error(__('用户不存在'),[],1);
        }
    }

    //4-读取加热机器人参数
    public function robot_param_get(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "fansInterval",
            "fansMaxTask",
            "videoInterval",
            "videoMaxTask",
            "videoMaxLikes",
            "videoMaxSpeak",
            "roomInterval",
            "roomMaxTask",
            "roomSpeakInterval",
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column);
        if(count($list)==1){
            $list = $list[0];
        }
        $this->success('返回成功', $list);
    }

    //5-读取话术库
    public function speak_list_get(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
            "speakType",
            "speakIn",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "speakOut",
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column);
        if(count($list)==1){
            $list = $list[0];
        }
        $this->success('返回成功', $list);
    }

    //6-读取用户权益描述=========================
    public function get_userright_describe(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userRightDescribe",
            "res",
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column);
        $this->success('返回成功', $list);
    }

    //7-读取VIP用户列表
    public function task_vip_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userCount"
        ];

        $in_out_column = [
            "userCount"
        ];
        //查询
        $list = $this->model->procedure_data_select2($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        $this->success('返回成功', $list);
    }
    //8-增加VIP用户
    public function vipuser_add(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "zone",
            "vipUser",
            "nickame",
            "avaterUrl",
            "gender",
            "grade",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('设置成功', []);
        }else{
            $this->error(__('用户数据没变化或用户不存在'),[],1);
        }
    }

    //9申请直播间加热--add
    public function task_roomheat_add(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "taskStarTime",
            "heatMinutes",
            "purposeOnlineNum",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('加热成功', []);
        }else{
            $this->error(__('加热失败'),[],1);
        }
    }

    //10读取直播间加热任务--select
    public function task_roomheat_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userCount"
        ];

        $in_out_column = [
            "userCount"
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'userCount'=>count($list),
            'list'=>$list,
        ];
        $this->success('返回成功', $data);
    }
    //11直播间加热明细登记==todo:存储过程缺少out输出
    public function task_roomheat_log(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "robotUser",
            "heatSeconds",
            "MsgNum",
            "costDiamonds",
            "likesNum",
            "currOnlineNum",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if(isset($rs['res']) && $rs['res']){
            $this->success('登记成功', []);
        }else{
            $this->error(__('登记失败'),[],1);
        }
    }
    //12读取直播间加热历史记录
    public function task_roomheat_history_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "taskCount"
        ];

        $in_out_column = [
            "taskCount"
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);

        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'taskCount'=>count($list),
            'list'=>$list,
        ];

        $this->success('返回成功', $data);
    }

    //13申请短视频加热--add
    public function task_videoheat_add(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "videoURL",
            "taskBeginTime",
            "heatMinutes",
            "taskMaxNum",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('加热成功', []);
        }else{
            $this->error(__('加热失败'),[],1);
        }
    }
    //14读取短视频加热任务--select
    public function task_videoheat_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userCount"
        ];

        $in_out_column = [
            "userCount"
        ];

        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'userCount'=>count($list),
            'list'=>$list,
        ];

        $this->success('返回成功', $data);
    }

    //15短视频加热登记--add
    public function task_videoheat_log(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "livestreamerUser",
            "videoUrl",
            "playSeconds",
            "isLike",
            "isCommend",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('登记成功', []);
        }else{
            $this->error(__('登记失败'),[],1);
        }
    }
    //16读取短视频加热记录--select
    public function task_videoheat_history_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "taskCount"
        ];

        $in_out_column = [
            "taskCount"
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);

        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'taskCount'=>count($list),
            'list'=>$list,
        ];

        $this->success('返回成功', $data);
    }

    //17申请加粉--add
    public function task_fans_add(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "currentFans",
            "taskBeginTime",
            "taskKeepDate",
            "taskFansNum",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if($rs['res']){
            $this->success('申请成功', []);
        }else{
            $this->error(__('申请失败'),[],1);
        }
    }

    //18读取加粉任务--select
    public function task_fans_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userCount"
        ];

        $in_out_column = [
            "userCount"
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
//my_print($list);die;

        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'userCount'=>count($list),
            'list'=>$list,
        ];

        if(isset($list['userCount'])){
            $data = [
                'userCount'=>$list['userCount'],
                'list'=>[],
            ];
        }


        $this->success('返回成功', $data);
    }

    //19加粉任务完成登记--add
    public function task_fans_log(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "robotUser",
            "livestreamerUser",
            "result",
        ];
        foreach ($must_param as $k=>$v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if(isset($rs['res']) && $rs['res']){
            $this->success('登记成功', []);
        }else{
            $this->error(__('登记失败'),[],1);
        }
    }

    //20加粉历史任务查询--select
    public function task_fans_history_qurey(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "tasktaskNum"
        ];

        $in_out_column = [
            "tasktaskNum"
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);

        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'tasktaskNum'=>count($list),
            'list'=>$list,
        ];

        if(isset($list['tasktaskNum'])){
            $data = [
                'tasktaskNum'=>$list['tasktaskNum'],
                'list'=>[],
            ];
        }


        $this->success('返回成功', $data);
    }

    //21读取培训课程目录--select
    public function dict_train_dir_list_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "zone",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "dirCount"
        ];

        $in_out_column = [
            "zone"
        ];



        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);


        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'zone'=>$params['zone'],
            'dirCount' => count($list),
            'list'=>$list,
        ];
        if(isset($list['zone'])){
            $data = [
                'zone'=>$list['zone'],
                'dirCount' => count($list),
                'list'=>[],
            ];
        }



        $this->success('返回成功', $data);
    }

    //22读取培训课程列表--select
    public function dict_train_course_list_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "dirID",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "courseCount"
        ];

        $in_out_column = [

        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        if(count($list)==1 && isset($list[0])) $list = $list[0];

        $data = [
            'courseCount'=>count($list),
            'list'=>$list,
        ];




        $this->success('返回成功', $data);
    }

    //23增加抢宝箱任务--add
    public function task_box_add(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "boxLeftTime",
            "onlineNum",
            "robotUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if(isset($rs['res']) && $rs['res']){
            $this->success('增加成功', []);
        }else{
            $this->error(__('增加失败'),[],1);
        }
    }

    //24登记抢宝箱明细--add
    public function task_box_log(){
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
            "robotUser",
            "getDiamonds",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $rs = $this->model->procedure_data_add($params,$must_param,__FUNCTION__);
        if(isset($rs['res']) && $rs['res']){
            $this->success('登记成功', []);
        }else{
            $this->error(__('登记失败'),[],1);
        }
    }

    //25读取抢宝箱任务--select
    public function task_box_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "robotUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "livestreamerUser",
            "boxTime",
        ];

        $in_out_column = [
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        $this->success('返回成功', $list);
    }
    //26读取抢宝箱历史记录--select
    public function task_box_history_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [

        ];

        $in_out_column = [
            "taskCount",
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);

        if(count($list) == 1 && isset($list[0])){
            $list = $list[0];
        }
        my_debug(1,'$list--344');
        my_debug(1,$list);
        $data = [
            'taskCount'=>count($list),
            'list'=>$list,
        ];
        if(isset($list['taskCount'])){
            $data = [
                'taskCount'=>$list['taskCount'],
                'list'=>[],
            ];
        }


        $this->success('返回成功', $data);
    }
    //27读取用户权益--select
    public function user_right_query(){
        //设置过滤方法
        $params = $this->request->post();
        $must_param = [
            "livestreamerUser",
        ];
        foreach ($must_param as $v){
            if(!isset($params[$v]) || empty($params[$v])){
                $this->error(__($v.'-是必传参数'),[],1);
            }
        }
        $out_column = [
            "userGrade",
            "roomheatSurplusNum",
            "videoheatSurplusNum",
            "vipSurplusNum",
            "fansSurplusNum",
            "res",
        ];

        $in_out_column = [
        ];
        //查询
        $list = $this->model->procedure_data_select($params,$must_param,__FUNCTION__,$out_column,$in_out_column);
        $this->success('返回成功', $list);
    }

}
